﻿using System.Reflection;

namespace 天离安全_天擎.Tl_DbContext_Api
{
    /// <summary>
    /// 公共服务，用于注册后续新服务
    /// </summary>
    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// 注册新服务
        /// </summary>
        /// <param name="services"></param>
        /// <param name="assembly"></param>
        public static void AddApplicationServices(this IServiceCollection services, Assembly assembly)
        {
            var serviceTypes = assembly.GetTypes()
                .Where(t => t.IsClass && !t.IsAbstract && t.Name.EndsWith("Services"));

            foreach (var serviceType in serviceTypes)
            {
                var interfaceType = serviceType.GetInterface($"{serviceType.Name}");
                if (interfaceType != null)
                {
                    services.AddScoped(interfaceType, serviceType);
                }
                else
                {
                    services.AddScoped(serviceType);
                }
            }
        }
    }
}
